package com.topisv.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.CustomersOrdenoSource;
import com.topisv.tms.repository.CustomersOrdenoSourceDao;

@Component
@Transactional(readOnly = true)
public class CustomersOrdenoSourceService {

	@Autowired
	private CustomersOrdenoSourceDao customersOrdenoSourceDao;
	
	public CustomersOrdenoSource findOne(Long id){
		return customersOrdenoSourceDao.findOne(id);
	}
	
	public List<CustomersOrdenoSource> findByCustomerCode(String customerCode){
		return customersOrdenoSourceDao.findByCustomerCode(customerCode);
	}
	
	private String getString(List<String> lists){
		if(lists==null||lists.isEmpty()){
			return null;
		}
		StringBuffer sb=new StringBuffer();
		for(String code:lists){
			if("".equals(sb.toString())){
				sb.append(code);
			}else{
				sb.append(":"+code);
			}
		}
		return sb.toString();
	}
	
	@Transactional(readOnly = false)
	public CustomersOrdenoSource save(CustomersOrdenoSource arg0){
		List<String> shopCodeLists=arg0.getShopCodeLists();
		if(shopCodeLists!=null){
			String shopCode=getString(shopCodeLists);
			if(shopCode!=null){
				arg0.setShopCodes(shopCode);
			}
		}
		List<String> mergeLists=arg0.getMergeLists();
		if(mergeLists!=null){
			String mergeCode=getString(mergeLists);
			if(mergeCode!=null){
				arg0.setMergeOrdenos(mergeCode);
			}
		}
		return customersOrdenoSourceDao.save(arg0);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id){
		customersOrdenoSourceDao.delete(id);
	}
}
